//
//  UIImage+SLSDK.m
//  SLSDK
//
//  Created by SongLian on 16/8/7.
//  Copyright © 2016年 SongLian. All rights reserved.
//

#import "UIImage+SLSDK.h"
#import <Accelerate/Accelerate.h>

@implementation UIImage (SLSDK)

+ (UIImage *)sl_imageWithSex:(NSString *)sex {
    UIImage *manImage = [UIImage imageNamed:@"Contacts_sex_man"];
    UIImage *womanImage = [UIImage imageNamed:@"Contacts_sex_woman"];
    return [sex isEqualToString:@"男"] ? manImage : womanImage;
}

+ (UIImage *)sl_imageWithVIPDegree:(NSString *)aVIPDegree {
    NSString *imageName = [NSString stringWithFormat:@"mine_vip%@", aVIPDegree];
    UIImage *aVIPImage = [UIImage imageNamed:imageName];
    return aVIPImage;
}

- (UIImage*)sl_scaledToSize:(CGSize)size {
    
    UIGraphicsBeginImageContext(size);
    [self drawInRect:CGRectMake(0,0,size.width,size.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

+ (UIImage *)sl_imageWithCrown:(NSInteger)crown {
    NSArray *nameArray = @[@"home_gold Crown", @"home_copperCrown", @"home_sliverCrown"];
    UIImage *image = [UIImage imageNamed:nameArray[crown]];
    return image;
}

- (UIImage *)sl_blurImageWithValue:(CGFloat)value {
    if (value < 0.f || value > 1.f)
        value = 0.5f;
    
    int boxSize = (int)(value * 40);
    boxSize = boxSize - (boxSize % 2) + 1;
    
    CGImageRef img = self.CGImage;
    
    vImage_Buffer inBuffer, outBuffer;
    vImage_Error error;
    
    void *pixelBuffer;
    //从CGImage中获取数据
    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
    //设置从CGImage获取对象的属性
    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);
    
    inBuffer.data = (void *)CFDataGetBytePtr(inBitmapData);
    
    pixelBuffer = malloc(CGImageGetBytesPerRow(img) *
                         CGImageGetHeight(img));
    
    if(pixelBuffer == NULL)
        NSLog(@"No pixelbuffer");
    
    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(img);
    outBuffer.height = CGImageGetHeight(img);
    outBuffer.rowBytes = CGImageGetBytesPerRow(img);
    
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    
    if (error)
        NSLog(@"error from convolution %ld", error);
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,
                                             outBuffer.width,
                                             outBuffer.height,
                                             8,
                                             outBuffer.rowBytes,
                                             colorSpace,
                                             kCGImageAlphaNoneSkipLast);
    CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
    
    //clean up
    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);
    
    free(pixelBuffer);
    CFRelease(inBitmapData);
    
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageRef);
    
    return returnImage;
}

@end
